package com.nttdata.ocr.util;

/**
 * OCR识别相关处理
 */
public final class OcrUtils {
    private static final String PREFIX_CHINESE_ID_NUMBER = "公民身份号码";

    /**
     * 车牌验证
     * @param plate
     * @return
     */
    public static boolean isPlate(String plate) {
        return isCommonPlate(plate) || isNewEnergyVehiclePlate(plate);
    }

    /**
     * 普通车牌
     * @param plate
     * @return
     */
    public static boolean isCommonPlate(String plate) {
        String newPattern = "^[京津晋冀蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新][ABCDEFGHJKLMNPQRSTUVWXY][1-9DF][0-9ABCDEFGHJKLMNPQRSTUVWXY]";
        return plate.matches(newPattern);
    }

    /**
     * 新能源车牌
     * @param plate
     * @return
     */
    public static boolean isNewEnergyVehiclePlate(String plate) {
        String newPattern = "^[京津晋冀蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新][ABCDEFGHJKLMNPQRSTUVWXY][1-9DF][0-9ABCDEFGHJKLMNPQRSTUVWXY]";
        return plate.matches(newPattern);
    }

    public static String replaceIdNumberPrefix(String idNumber) {
        return idNumber.replace(PREFIX_CHINESE_ID_NUMBER, "");
    }

    public static boolean isIdNumber(String idNumber) {
        if (idNumber.startsWith(PREFIX_CHINESE_ID_NUMBER)) {
            idNumber = idNumber.replace(PREFIX_CHINESE_ID_NUMBER, "");
        }
        String pattern = "^[1-9]\\d{5}(19|20)\\d{2}((0[1-9])|(10||11||12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
        return idNumber.matches(pattern);
    }


}
